<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../public/jquery-3.4.1.js"></script>
</head>

<body>

</body>
<script>
    function request(flag = 1) {
        return new Promise((resolve, reject) => {
            if (flag == 1) {
                resolve({
                    msg: "成功",
                    status: 200
                })
            } else {
                reject({
                    msg: "失败",
                    status: 400
                })
            }
        })
    }
    request().then(res => {
        console.log(res)
    }).catch(error => {
        console.log(error)
    })

    //jQueryajax请求的步骤
    $.ajax({
        url: "/city.json",
        type: "get",
        data: {},
        dataType: "json",
        success: (res) => {
            console.log(res)
        },
        error: (error) => {
            console.log(error);
        }
    })

    //原生ajax的请求步骤
    function axios({ url }) {
        return new Promise((resolve, reject) => {
            let xhr = null;
            //1. 实例化一个对象
            xhr = new XMLHttpRequest();
            //2. open方法打开地址
            xhr.open("get", url);
            //3.设置header头，
            // xhr.setRequestHeader("content-type",'application/json')
            //4. 设置回调函数
            xhr.onreadystatechange = (res) => {
                if (res.target.status == 200 && res.target.readyState == 4) {
                    resolve({
                        msg: "请求成功",
                        data: JSON.parse(res.target.response),
                        status: res.target.status
                    })
                } else {
                    // reject({
                    //     msg: "请求失败",
                    //     status: 400
                    // })
                }
            }
            //5.send发送请求
            xhr.send()
        })
    }
    axios({url:"/city.json"}).then(res=>{
        console.log(res);
    })
</script>

</html>